package com.hyts.infodb.zuul.service;
import com.google.common.collect.Lists;
import com.hyts.infodb.error.subclass.ServiceException;
import com.hyts.infodb.zuul.bean.ZuulRouteBO;
import com.hyts.infodb.zuul.bean.ZuulRoutePO;
import com.hyts.infodb.zuul.bean.ZuulRouteVO;
import com.hyts.infodb.zuul.dao.ZuulDAO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @project-name:infodb
 * @package-name:com.hyts.infodb.zuul.service
 * @author:Libo/Alex
 * @create-date:2019-05-22 20:54
 * @copyright:libo-hyts-github
 * @email:libo2dev@aliyun.com
 * @description:此类主要用于: zuul 路由服务业务逻辑对象实体业务类
 */
@Service
public class ZuulService {


    @Autowired
    ZuulDAO zuulDAO;

    /**
     * 获取所有的路由信息存储在db中的
     * @return
     */
    public List<ZuulProperties.ZuulRoute> findAllRoute(){
        List<ZuulRoutePO> zuulRoutePOList = zuulDAO.findAll();
        if(CollectionUtils.isNotEmpty(zuulRoutePOList)){
            return zuulRoutePOList.parallelStream().map(param-> {
               ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
               BeanUtils.copyProperties(param,zuulRoute);
               zuulRoute.setId(param.getId()+"");
               return zuulRoute;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    /**
     *  add 保存路由功能
     * @param zuulRouteDTO
     * @return
     */
    public boolean save(ZuulRouteBO zuulRouteDTO){
        if(Objects.isNull(zuulRouteDTO)){
            throw new ServiceException("传入的ZuulRouteBO不可以为空!");
        }
        ZuulRoutePO zuulRoutePO = new ZuulRoutePO();
        BeanUtils.copyProperties(zuulRouteDTO,zuulRoutePO);
        return Optional.ofNullable(zuulDAO.save(zuulRoutePO)).isPresent();
    }

    /**
     * 删除路由功能
     * @param zuulRouteDTO
     * @return
     */
    public boolean remove(ZuulRouteBO zuulRouteDTO){
        if(Objects.isNull(zuulRouteDTO)){
            throw new ServiceException("传入的ZuulRouteBO不可以为空!");
        }
        ZuulRoutePO zuulRoutePO = new ZuulRoutePO();
        BeanUtils.copyProperties(zuulRouteDTO,zuulRoutePO);
        try {
            zuulDAO.delete(zuulRoutePO);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 加载路由功能
     * @param zuulRouteDTO
     * @return
     */
    public ZuulRouteVO load(ZuulRouteBO zuulRouteDTO){
        if(Objects.isNull(zuulRouteDTO)){
            throw new ServiceException("传入的ZuulRouteBO不可以为空!");
        }
        Optional<ZuulRoutePO> zuulRoutePO = zuulDAO.findById(zuulRouteDTO.getId());
        BeanUtils.copyProperties(zuulRouteDTO,zuulRoutePO);
        ZuulRouteVO zuulRouteVO = new ZuulRouteVO();
        if(zuulRoutePO.isPresent()){
            zuulRouteVO.setContent(zuulRoutePO.get());
        }
        return zuulRouteVO;
    }


    /**
     * 分页查询
     * @param zuulRouteBO
     * @return
     */
    public ZuulRouteVO find(ZuulRouteBO zuulRouteBO){
        ZuulRouteVO vo = new ZuulRouteVO();
        return (ZuulRouteVO) vo.setPageSubject(zuulDAO
                .findAll(zuulRouteBO.buildPageData(zuulRouteBO.getPageNum(),
                        zuulRouteBO.getPageSize())));
    }

    /**
     * @param zuulRouteDTO
     * @return
     */
    public void validate(ZuulRouteBO zuulRouteDTO) throws ServiceException{
        if(Objects.nonNull(zuulRouteDTO.getId())) {
            int repeateName = zuulDAO.countZuulRoutePOByIdNotAndName(zuulRouteDTO.getId(), zuulRouteDTO.getName());
            if (repeateName != 0) {
                throw new ServiceException("以及存在相同的名称的路由配置!");
            }
            int repeatePath = zuulDAO.countZuulRoutePOByIdNotAndName(zuulRouteDTO.getId(), zuulRouteDTO.getName());
            if (repeatePath != 0) {
                throw new ServiceException("以及存在相同的路由的路由配置!");
            }
        }else{
            int repeateName = zuulDAO.countZuulRoutePOByName(zuulRouteDTO.getName());
            if (repeateName != 0) {
                throw new ServiceException("以及存在相同的名称的路由配置!");
            }
            int repeatePath = zuulDAO.countZuulRoutePOByName(zuulRouteDTO.getName());
            if (repeatePath != 0) {
                throw new ServiceException("以及存在相同的路由的路由配置!");
            }
        }
    }

}
